package com.winit.indexing.utils;

import org.springframework.util.CollectionUtils;

import java.util.*;

public class IndexingUtil {
    /**
     * 取map并集，相同key情况下priorMap优先级高于baseMap，这里的操作不会改变传入参数
     *
     * @param baseMap
     * @param priorMap
     * @return 返回新map
     */
    public static Map<String, String> merge(Map<String, String> baseMap, Map<String, String> priorMap) {
        Map<String, String> rs = new LinkedHashMap<>();
        if (null != baseMap) {
            rs.putAll(baseMap);
        }
        if (null != priorMap) {
            rs.putAll(priorMap);
        }
        return rs;
    }

    /**
     * 取map交集，相同key情况下priorMap优先级高于baseMap，这里的操作不会改变传入参数
     *
     * @param baseMap
     * @param priorMap
     * @return 返回新的map
     */
    public static Map<String, String> interset(Map<String, String> baseMap, Map<String, String> priorMap) {
        Map<String, String> rs = new HashMap<String, String>();
        if (CollectionUtils.isEmpty(baseMap) || CollectionUtils.isEmpty(priorMap)){
            return rs;
        }
        Map<String, String> tempMap = new HashMap<>();
        tempMap.putAll(baseMap);
        Set<String> set = tempMap.keySet();
        boolean intersect = set.retainAll(priorMap.keySet());
        if (intersect){
            for (String s : set){
                rs.put(s, priorMap.get(s));
            }
        }
        return rs;
    }

//    public static void main(String[] args) {
//        Map<String, String> map1 = new HashMap<>();
//        Map<String, String> map2 = new HashMap<>();
//        map1.put("1", "1");
//        map1.put("2", "2");
//        map1.put("3", "3");
//        map2.put("2", "2");
//        map2.put("3", "3");
//        map2.put("4", "4");
//        Set<String> set1 = map1.keySet();
//        System.out.println(set1.retainAll(map2.keySet()));
//        System.out.println(set1);
//        System.out.println(map1.keySet());
//        System.out.println(map1);
//
////        Set<String> set1 = new HashSet<>();
////        Set<String> set2 = new HashSet<>();
////        set1.add("1");
////        set1.add("2");
////        set1.add("3");
////        set2.add("2");
////        set2.add("3");
////        set2.add("4");
////        System.out.println(set1.retainAll(set2));
////        System.out.println(set1);
////        System.out.println(set2);
//    }
}
